
print("…3API 可以访问gitee了哦，点右上角的蜂窝跳转")
print("…3ESP8266需要刷入固定的固件 可以访问gitee查阅，点右上角的蜂窝跳转")

const toolmenu = libs_inthis.findViewById(com.seleuco.foru.R.id.toolbarmenu)
libs_inthis.runOnUiThread(function(){
        toolmenu.addMenu(com.seleuco.foru.R.mipmap.module,"访问Gitee",()=>{
            App.getApp(libs_inthis).openBrowser("https://gitee.com/lingsatuo/esp8266-lib-for-foxjavascript")
        })
        toolmenu.addMenu(com.seleuco.foru.R.mipmap.module,"固件代码 Gitee",()=>{
            App.getApp(libs_inthis).openBrowser("https://gitee.com/lingsatuo/esp8266-for-foxjavascript")
        }) 
        toolmenu.addMenu(com.seleuco.foru.R.mipmap.module,"查看ESP8266 API文档",()=>{
            App.getApp(libs_inthis).openBrowser("https://www.showdoc.com.cn/Celivad/7054201589038271")
        })
})

const Device = []
const INPUT = "INPUT"
const OUTPUT = "OUTPUT"
const D0 = 16
const D1 = 5
const D2 = 4
const D3 = 0
const D4 = 2
const D5 = 14
const D6 = 12
const D7 = 13
const D8 = 15
const LED_BUILTIN = 2
const HIGH = 1
const LOW = 0
const Board={}
var DEBUG = false
/**
 * 开放端口并等待连接
 * @param {number} port 端口号
 */
function accept(port){
    var connect = WIFI.getConnect().accept(port)
    var message = connect.readLine()
    if(message!=null){
        print(message)
    }
    var dv = JSON.parse(sendCommand("id=001&hi",connect))
    var device = {
        PORT:port,
        NAME:dv.name,
        CONNECT:connect,
        IP:connect.getIp(),
        write:(what)=>{connect.write(""+what+"\r")},
        readLine:()=>{return connect.readLine()}
    }
    Device.push(device)
}



/**
 * 判断设备是否还连接着
 * @param {String} ip 设备的IP
 */

function isConnected(ip){
    var ips = WIFI.getIps()
    for(var i in ips){
        var device = ips[i]
        if(device.IP==ip) return true
    }
    return false
}

/**
 * 获取设备
 * @param {number} index 获取第几个
 */
const getDevice = function(index){
    if(typeof(index)=="number"){
        return Device[index]    
    }else{
        for(var i = 0;i<Device.length ;i++ ){
            var device = getDevice(i)
            if(device.NAME == index) return device
        }
    }
}


/**
 * 给指定设备发送命令，但不等待结果
 * @param {String} command 
 * @param {Device} device 
 */
 function postCommand(command,device){
    // if(!isConnected(device.IP)) throw "该设备未连接"
    var d = device==undefined?getDevice(0):device
    if(DEBUG){
        if(d!=undefined)
        print(d.NAME+"-->"+command)
        else{
            print(d+"-->"+command)
        }
    }
    d.write(command)
}


/**
 * 等待指定设备返回的指定结果
 * @param {String} id 
 * @param {Device} device 
 */
 function waitCommand(id,device,time){
    // if(!isConnected(device.IP)) throw "该设备未连接"
    var d = device==undefined?getDevice(0):device
    var back = ""
    if(DEBUG){
        print(d.NAME+"<--"+back)
    }
    wait(()=>{
        back = d.readLine()
        var json = JSON.parse(back)
        if(!json.result&&json.id==id){
            var success = d.readLine()
            if(DEBUG){
                print(d.NAME+"<--"+success)
            }
            return true
        }
        return false
    },time==undefined?5000:time)
    return back
}



/**
 * 给指定设备发送命令并等待结果
 * @param {String} command 
 * @param {Device} device 
 */
function sendCommand(command,device){
    // if(!isConnected(device.IP)) throw "该设备未连接"
    var d = device==undefined?getDevice(0):device
    if(DEBUG){
        if(d!=undefined)
        print(d.NAME+"-->"+command)
        else{
            print(d+"-->"+command)
        }
    }
    d.write(command)
    var back = d.readLine()
    if(DEBUG){
        print(d.NAME+"<--"+back)
    }
    if(!JSON.parse(back).result){
        var success = d.readLine()
        if(DEBUG){
            print(d.NAME+"<--"+success)
        }
    }
    return back
}


function pinMode(pin,mode,device){
    var d = device==undefined?getDevice(0):device
    sendCommand("id=002&M"+pin+"="+mode,d)
}


function delay(time,device){
    if(time<=0) throw "休眠时间小于1 无意义 delay"
    var d = device==undefined?getDevice(0):device
    sendCommand("id=003&D"+"="+time,d)
}

function delayMicroseconds(time,device){
    if(time<=0) throw "休眠时间小于1 无意义 delayMicroseconds"
    var d = device==undefined?getDevice(0):device
    sendCommand("id=004&d"+"="+time,d)
}


function digitalWrite(pin,mode,device){
    var d = device==undefined?getDevice(0):device
    sendCommand("id=005&S"+pin+"="+mode,d)
}
function pwm(pin,hz,device){
    var d = device==undefined?getDevice(0):device
    sendCommand("id=006&P"+pin+"="+hz,d)
}


function analogWrite(pin,mode,device){
    var d = device==undefined?getDevice(0):device
    sendCommand("id=008&A"+pin+"="+mode,d)
}


function analogRead(pin,device){
    var d = device==undefined?getDevice(0):device
    var message = sendCommand("id=007&ReadAll",d)
    var obj = JSON.parse(message)
    switch(pin){
        case D0:
            return obj.D0;
        case D1:
            return obj.D1;
        case D2:
            return obj.D2;
        case D3:
            return obj.D3;
        case D4:
            return obj.D4;
        case D5:
            return obj.D5;
        case D6:
            return obj.D6;
        case D7:
            return obj.D7;
        case D8:
            return obj.D8;
        case LED_BUILTIN:
                return obj.LED_BUILTIN;
    }
    throw "不支持"+pin+"引脚"
}

function digitalRead(pin,device){
    var d = device==undefined?getDevice(0):device
    var message = sendCommand("id=007&GetAll",d)
    var obj = JSON.parse(message)
    switch(pin){
        case D0:
            return obj.D0;
        case D1:
            return obj.D1;
        case D2:
            return obj.D2;
        case D3:
            return obj.D3;
        case D4:
            return obj.D4;
        case D5:
            return obj.D5;
        case D6:
            return obj.D6;
        case D7:
            return obj.D7;
        case D8:
            return obj.D8;
        case LED_BUILTIN:
                return obj.LED_BUILTIN;
    }
    throw "不支持"+pin+"引脚"
}